<?php

namespace IDock\Insurance\ZFIC\Response;

use IDock\Http\Request;
use IDock\Http\Response;

class AllZFICResponse extends Response
{

    protected $status = '';

    protected $logicStatus = '';

    public function resolve(): Response
    {
        $body = $this->getBody();

        if ($body) {
            $body = json_decode($body,true);
            $errorCode = $body['state'] ?? '';
            $error = $body['msg'] ?? '';

            if ($errorCode != '200' && $errorCode != '1005' && (strpos($error,'重复投保') === false)) {
                $this->setError($error);
                return  $this;
            }

            $this->setBody($body);
        }

        return $this;
    }

    public function responseLogs($uuid, $data)
    {
        if (!$this->logsFile) {
            return;
        }
        $data = str_replace(["\r\n"],[''],$data);
        $repLogs = date('Y-m-d H:i:s') . " RESPONSE " . $uuid . " " . $data;

        file_put_contents($this->logsFile, $repLogs . "\r\n", FILE_APPEND);
    }

    /**
     * @return string
     */
    public function getState(): string
    {
        return $this->get('state','');
    }

    /**
     * @return string
     */
    public function getMsg(): string
    {
        return $this->get('msg','');
    }


}